package updown.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import updown.constant.LogConstant;
import updown.controller.other.BaseController;
import updown.domain.MyInstanceInfo;
import updown.service.updown.EurekaService;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>Title: ww.kaixin.downupservice.controller.other </p>
 * <p>Description:to-22-6-1: -  </p>
 *
 * @Author:jiangkaixin
 * @Date:2021/12/23
 * @Version: 1.0
 */
@Slf4j
@Controller
@RequestMapping("/eurekaService")
public class EurekaController extends BaseController {

    @Autowired
    private EurekaService eurekaService;

    /**
     * 从 eureka 中拉取获取来的 service 信息
     * 1. 真实Eureka 中的 list是从
     * @param request
     * @param model
     * @return
     */
    @GetMapping(path = "/servicesInEurekaServer")
    public String servicesInEurekaServer(HttpServletRequest request, Map<String, Object> model) {
        log.info("log.info:servicesInEurekaServer >>>:{}");
        eurekaService.servicesInEurekaServer(model);
        return "eurekaServiceMap";
    }

    /**
     * 从 eureka 中拉取获取来的 service 信息
     * 1. 真实Eureka 中的 list是从
     * @param request
     * @param model
     * @return
     */
    @ResponseBody
    @GetMapping(path = "/getSpecificOne")
    public MyInstanceInfo getSpecificOne(@RequestParam(value = "serviceName") String serviceName, @RequestParam(value = "instanceId")String instanceId) {
        log.info("getSpecificOne:{},instanceId:{}",serviceName,instanceId);
        MyInstanceInfo instanceInfo = eurekaService.getSpecificInstanceByNameAndInstanceId(serviceName, instanceId);
        return instanceInfo;
    }

    /**
     * 从 eureka 中拉取获取来的 service 信息
     * 1. 真实Eureka 中的 list是从
     * @param request
     * @param model
     * @return
     */
    @ResponseBody
    @GetMapping(path = "/sendDownUPForSpecificOne")
    public String sendDownUPForSpecificOne(@RequestParam(value = "serviceName") String serviceName,
                                           @RequestParam(value = "instanceId")String instanceId,
                                           @RequestParam(value = "status")String status) {
        log.info("sendDownForSpecificOne:serviceName:{},instanceId:{},status:{}",serviceName,instanceId,status);
        Boolean success = eurekaService.sendRequestAndWaitCheckStatus(instanceId, serviceName, status);
        addLog("发送状态 " + serviceName + " 修改操作：" + status, LogConstant.MODULE_EUREKA);
        if (success){
            return "状态更改请求成功";
        }
        return "状态更改请求失败";
    }
}
